﻿using System;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace MRegularExpression
{
    public partial class RegexTESTER : Form
    {
        public RegexTESTER()
        {
            InitializeComponent();
        }

        private void btnMatch_Click(object sender, EventArgs e)
        {
            RegexOptions option = RegexOptions.None;
            if (cbIgnoreCase.Checked) option |= RegexOptions.IgnoreCase;
            if (cbSingle.Checked) option |= RegexOptions.Singleline;
            if (cbRight2Left.Checked) option |= RegexOptions.RightToLeft;
            if (cbMultiline.Checked) option |= RegexOptions.Multiline;

            Regex regex = new Regex(txtPattern.Text, option);
            MatchCollection matches = regex.Matches(txtSource.Text);
            StringBuilder resultBuilder = new StringBuilder();
            foreach (Match match in matches)
            {
                resultBuilder.AppendFormat("{0}\r\n\r\n", match.Value);
            }
            txtMatch.Text = resultBuilder.ToString();
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            RegexOptions option = RegexOptions.None;
            if (cbIgnoreCase.Checked) option |= RegexOptions.IgnoreCase;
            if (cbSingle.Checked) option |= RegexOptions.Singleline;
            if (cbRight2Left.Checked) option |= RegexOptions.RightToLeft;
            if (cbMultiline.Checked) option |= RegexOptions.Multiline;

            Regex regex = new Regex(txtPattern.Text, option);
            txtMatch.Text = regex.Replace(txtSource.Text, txtReplacement.Text);
        }

        private void btnClearLeft_Click(object sender, EventArgs e)
        {
            txtSource.Text = string.Empty;
        }

        private void btnClearRight_Click(object sender, EventArgs e)
        {
            txtMatch.Text = string.Empty;
        }

        private void btnMoveToLeft_Click(object sender, EventArgs e)
        {
            txtSource.Text = txtMatch.Text;
        }
    }
}
